package com.lixueju.security.box.web.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lixueju.security.box.core.model.AuthMenu;
import com.lixueju.security.box.web.service.IAuthMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 储存菜单信息 前端控制器
 * </p>
 *
 * @author lixueju
 * @since 2019-11-13
 */
@RestController
@RequestMapping(value = "/security/box/authMenu", produces = "application/json;charset=utf-8")
public class AuthMenuController extends SecurityBoxBaseController {
    @Autowired
    private IAuthMenuService authMenuService;

    /**
     * 动态菜单
     */
    @GetMapping("/getUserMenuList")
    public String getUserMenuList() {
        return callbackSuccess(authMenuService.queryAllMenuByUserId(getUserId()));
    }

    /**
     * 动态菜单和权限码集合
     */
    @GetMapping("/getUserMenuListAndPermissions")
    public String getUserMenuListAndPermissions() {
        JSONObject result = new JSONObject();
        Long userId = getUserId();
        result.put("menuList", authMenuService.queryAllMenuByUserId(userId));
        result.put("permissions", authMenuService.queryAllPermissionByUserId(userId));
        return callbackSuccess(result);
    }

    /**
     * 获取当前用户所有权限码
     */
    @GetMapping("/getUserPermission")
    public String getUserPermission() {
        return callbackSuccess(authMenuService.queryAllPermissionByUserId(getUserId()));
    }

    /**
     * 菜单列表
     */
    //@PreAuthorize("hasAuthority('admin')")
    @GetMapping("/menuList")
    public String menuList(AuthMenu authMenu) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object details = authentication.getDetails();
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(details));
        Jwt jwt = JwtHelper.decodeAndVerify(jsonObject.getString("tokenValue"), new RsaVerifier(jwtAccessTokenConverter.getKey().get("value")));
        JSONObject claimsJson = JSONObject.parseObject(jwt.getClaims());
        Page<AuthMenu> page = getPage(true);
        return callbackSuccess(authMenuService.selectMenuPage(page, authMenu));
    }

    /**
     * 新增or修改
     */
    @PostMapping("/addOrUpdateAuthMenu")
    public String addOrUpdateAuthMenu(@RequestBody AuthMenu authMenu) {
        return callbackSuccess(authMenuService.addOrUpdateAuthMenu(authMenu));
    }

    @PostMapping("/deleteAuthMenu")
    public String deleteAuthMenu(@RequestBody AuthMenu authMenu) {
        return callbackSuccess(authMenuService.deleteAuthMenu(authMenu));
    }

    /**
     * 树结构菜单
     */
    @GetMapping("/menuTree")
    public String menuTree() {
        return callbackSuccess(authMenuService.menuTree());
    }

    /**
     * 树结构菜单
     */
    @GetMapping("/menuTreeForPermission")
    public String menuTreeForPermission() {
        return callbackSuccess(authMenuService.menuTreeForPermission());
    }
}

